#include "win3_mment.h"
#include <sys/types.h>
#include <dirent.h>

lv_obj_t * win3;
lv_obj_t * win3_commodity; // 商品窗口
lv_obj_t ** commodity_btn;
//static lv_style_t style1;
int style_flag = 0;
static lv_obj_t *list;  //列表对象


static void btn_event_cb(lv_event_t * e)
{
    lv_event_code_t code = lv_event_get_code(e);  
    if(code == LV_EVENT_CLICKED)  
    {
        lv_obj_del(win3);
        win3_user_face();
    }
}

static void get_maftion_event_cb(lv_event_t * e)
{
    lv_event_code_t code = lv_event_get_code(e);  
    if(code == LV_EVENT_CLICKED)  
    {
        lv_obj_del(win3_commodity);
       // RDWR_get_new(NULL);
        win3_new();
    }
}

static void  WIN3_file_btn_cd(lv_event_t *e)
{
    lv_event_code_t event_code = lv_event_get_code(e);
    lv_obj_t * btn = lv_event_get_target(e);

    if(event_code == LV_EVENT_SHORT_CLICKED) 
    {
        char *file_name;
        file_name = (char *)lv_list_get_btn_text(list, btn);
        printf("file_name = %s\n", file_name);
        //去掉PNG后缀
        char *file_name1 = strtok(file_name, ".");
        printf("%s\n",file_name1);
        go_on(file_name1);

    }
    else if (event_code == LV_EVENT_LONG_PRESSED)
    {
        char *file_name;
        file_name = (char *)lv_list_get_btn_text(list, btn);
        go_file(file_name);
    }
    
}
static void commodity_btn_cb(lv_event_t * e) // 按钮事件函数
{
    lv_event_code_t code = lv_event_get_code(e); // 同一个按钮发出不同不同事件
    lv_obj_t * event_btn = lv_event_get_target(e);
    int i                = 0;
    while(commodity_btn[i] != NULL) 
    {
        if(event_btn == commodity_btn[i])
         {
            printf("开始解析\n");
            FILE * fd =fopen("/home/gec/samba_share/001__lv_port_linux_ubuntu_sdl/my_project/Data/commodity_new.txt", "r+");
            if(fd == NULL) 
            {
                printf("open file error\n");
                break;
            }
            char buf[128] = {0};
            for(int j = 0; j <= i; j++) {
                fgets(buf, 100, fd);
            }
            take_off(buf);
            break;
        }
        i++;
        printf("i=%d\n", i);
    }
}

void win3_mment(void)
{
    pthread_t new_pd;
    pthread_create(&new_pd, NULL, RDWR_get_new, NULL);
    sleep(3);
    // lv_font_t * font = lv_freetype_font_create(
    //     "/home/gec/samba_share/001__lv_port_linux_ubuntu_sdl/my_project/Data/simkai.ttf", // 字体文件路径
    //     LV_FREETYPE_FONT_RENDER_MODE_BITMAP, 24, LV_FREETYPE_FONT_STYLE_NORMAL);

    // if(!font) {
    //     LV_LOG_ERROR("freetype font create failed.");
    //     return;
    // }
    // if(style_flag == 0) {
    //     /*Create style with the new font*/
    //     lv_style_init(&style1);
    //     lv_style_set_text_font(&style1, font);
    //     lv_style_set_text_align(&style1, LV_TEXT_ALIGN_CENTER);
    // }
    win3 = lv_obj_create(lv_scr_act());
    lv_obj_set_size(win3, 800, 480);
    lv_obj_set_style_pad_all(win3, 0, 0);
    lv_obj_clear_flag(win3, LV_OBJ_FLAG_SCROLLABLE);


    lv_obj_t *get_maftion = lv_obj_create(win3);
    lv_obj_set_size(get_maftion, 130, 80);
    lv_obj_set_style_pad_all(get_maftion,0,0);
    lv_obj_set_style_bg_color(get_maftion, lv_color_hex(0xFFFFFF), LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_align(get_maftion,  LV_ALIGN_TOP_RIGHT, 0, 0);

    lv_obj_t *btn = lv_btn_create(get_maftion);
    lv_obj_set_size(btn, 65, 80);
    lv_obj_set_align(btn, LV_ALIGN_RIGHT_MID);
    lv_obj_set_style_bg_color(btn, lv_color_hex(0xFFFFFF), LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_add_event_cb(btn, btn_event_cb, LV_EVENT_CLICKED, NULL);
    lv_obj_t* img1 = lv_img_create(btn);
    LV_IMG_DECLARE(out);
    lv_img_set_src(img1, &out);
    lv_obj_set_align(img1, LV_ALIGN_CENTER);

    lv_obj_t *btn1 = lv_btn_create(get_maftion);
    lv_obj_set_size(btn1, 65, 80);
    lv_obj_set_align(btn1, LV_ALIGN_LEFT_MID);
    lv_obj_set_style_bg_color(btn1, lv_color_hex(0xFFFFFF), LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_add_event_cb(btn1, get_maftion_event_cb, LV_EVENT_CLICKED, NULL);
    lv_obj_t* img = lv_img_create(btn1);
    LV_IMG_DECLARE(refresh);
    lv_img_set_src(img, &refresh);
    lv_obj_set_align(img, LV_ALIGN_CENTER);
    lv_img_set_zoom(img, 300);

    lv_obj_t *btn2 = lv_btn_create(win3);
    lv_obj_set_size(btn2, 130, 50);
    lv_obj_align(btn2, LV_ALIGN_BOTTOM_RIGHT,0,-200);
    lv_obj_t * price_label = lv_label_create(btn2);
    lv_label_set_text_fmt(price_label, "Go on sale");
    lv_obj_align(price_label, LV_ALIGN_CENTER, 0,0);
    WIN3_list_btn( );
    win3_new();
   

}



void win3_new(void)
{

    win3_commodity = lv_obj_create(win3); // 商品列表区
    lv_obj_set_size(win3_commodity, 670, 480);
    lv_obj_set_scroll_dir(win3_commodity, LV_DIR_VER);
    lv_obj_set_style_pad_all(win3_commodity, 0, 0);
    
    lv_obj_set_style_bg_color(win3_commodity, lv_color_hex(0xFFFFFF), LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_align(win3_commodity, LV_ALIGN_BOTTOM_LEFT);
    commodity_btn = malloc(sizeof(lv_obj_t *) * WIN3_COMMODITY_NUM);
    int j         = 0;
    int x         = 5;
    int y         = 5;
    int set_x     = 0;
    FILE * fd = fopen("/home/gec/samba_share/001__lv_port_linux_ubuntu_sdl/my_project/Data/commodity_new.txt", "r+");
    if(fd == NULL) {
        printf("open file error\n");
        return 0;
    }

    char buf[255];
    while(1) {
        char number[128]        = {0};
        char num[128]           = {0};
        char picture_path[1024] = {0};

        char * set = fgets(buf, 255, fd);
        if(set == NULL) {
            break;
        }
        char * p1 = strtok(buf, ",");
        int i     = 0;
        while(p1 != NULL) {
            if(i == 0)
                ;
            else if(i == 1)
                strcpy(number, p1);
            else if(i == 2)
                strcpy(num, p1);
            i++;
            p1 = strtok(NULL, ",");
        } 
        lv_obj_t * commodity = lv_obj_create(win3_commodity); // 这个全局变量可能没用
        lv_obj_set_size(commodity, 150, 200);
        lv_obj_set_style_pad_all(commodity, 0, 0);
        lv_obj_clear_flag(commodity, LV_OBJ_FLAG_SCROLLABLE);
        lv_obj_set_style_bg_color(commodity, lv_color_hex(0xFFFFFF), LV_PART_MAIN | LV_STATE_DEFAULT);
        lv_obj_align(commodity, LV_ALIGN_TOP_LEFT, x, y);
        sprintf(picture_path,
                "A:/home/gec/samba_share/001__lv_port_linux_ubuntu_sdl/my_project/Data/commodity_picture/%s.png",
                number);
        lv_obj_t * img;
        img = lv_image_create(commodity);
        lv_image_set_src(img, picture_path);
        lv_obj_align(img, LV_ALIGN_TOP_MID, 0, 0);
        // 使用局部变量来存储按钮的索引
        commodity_btn[j] = lv_btn_create(commodity);
        lv_obj_set_size(commodity_btn[j], 80, 40);
        lv_obj_align(commodity_btn[j], LV_ALIGN_BOTTOM_RIGHT, 0, 0);
        lv_obj_t * btn_label = lv_label_create(commodity_btn[j]);
        //lv_obj_add_style(btn_label, &style1, 0);
        lv_label_set_text(btn_label, "removed");
        lv_obj_add_event_cb(commodity_btn[j], commodity_btn_cb, LV_EVENT_CLICKED, NULL);
        lv_obj_t * price = lv_obj_create(commodity);
        lv_obj_clear_flag(price, LV_OBJ_FLAG_SCROLLABLE);
        lv_obj_set_style_pad_all(price, 0, 0);
        lv_obj_set_size(price, 70, 40);
        
        lv_obj_align(price, LV_ALIGN_BOTTOM_LEFT, 0, 0);
        lv_obj_t * price_label = lv_label_create(price);
        //lv_obj_add_style(price_label, &style1, 0);
        lv_label_set_text_fmt(price_label, "%s", num);
        lv_obj_align(price_label, LV_ALIGN_CENTER, 0, 10);
        j++;
        if(set_x < 3) {
            set_x++;
            x = x + 160;
        } else {
            set_x = 0;
            x     = 10;
            y     = y + 210;
        }
    }
    fclose(fd);
}
void WIN3_list_btn( )
{
    //列表
    list = lv_list_create(win3);
    lv_obj_set_size(list, 110, 200);
    lv_obj_align(list, LV_ALIGN_BOTTOM_RIGHT,-10,0);
    DIR* dp = opendir("/home/gec/samba_share/001__lv_port_linux_ubuntu_sdl/my_project/Data/go_on");
    if(dp == NULL)
    {
        //printf("PROBLEM: opendir %s error; REASON: %s; LINE: %d;\n",strerror(errno), __LINE__);
        return ;
    }
    // 2、读取目录文件信息，将里面的信息打印出来
    struct dirent *ds_msg_p = NULL;     // 定义目录属性结构体变量
    //struct stat info;                   // 定义文件属性结构体变量
    char buf[128] = {0}; 
    int i=0;      
    while(1)
    {
        ds_msg_p = readdir(dp);
        if(ds_msg_p == NULL)
           break;
        // B、判断不是"."和".."的信息
        if(strcmp(ds_msg_p->d_name, ".")!=0 && strcmp(ds_msg_p->d_name, "..")!=0)
        {
            // 判断是普通文件的信息
            if(ds_msg_p->d_type == DT_REG)  
            {
                lv_obj_t *btn = lv_list_add_btn(list, NULL, ds_msg_p->d_name);
                lv_obj_add_event_cb(btn, WIN3_file_btn_cd,  LV_EVENT_ALL, NULL); //添加事件回调
            }

        }
    }
}
